package com.ballmaze.maze;

import java.util.ArrayList;
import java.util.List;

public class Player {
    private int id;
    private MazeLocation location;
    private float power;
    private boolean alive;

    public Player(int x, int y, float power, int id) {
        this.id = id;
        this.location = new MazeLocation(y, x);
        this.power = power;
        this.alive = true;
    }

    @Override
    public String toString() {
        return "Player{" +
                "x=" + location.getX() +
                ", y=" + location.getY() +
                ", power=" + power +
                '}';
    }

    public MazeLocation getLocation() {
        return location;
    }

    public int getX() {
        return location.getX();
    }

    public void setX(int x) {
        this.location.setX(x);
    }

    public int getY() {
        return location.getY();
    }

    public void setY(int y) {
        this.location.setY(y);
    }

    public float getPower() {
        return power;
    }

    public void setPower(float power) {
        this.power = power;
    }

    public String getPositionStr() {
        return this.location.getX() + ":" + this.location.getY();
    }

    public boolean isAlive() {
        return alive;
    }

    public void setAlive(boolean alive) {
        this.alive = alive;
    }

    public int getId() {
        return id;
    }

    /**
     * 返回胜负数组，数组第一个元素是胜者、第二个元素是败者
     *
     * @param a 玩家A
     * @param b 玩家B
     * @return 胜负者数组
     */
    public static BattleResult battle(Player a, Player b) {
        return new BattleResult(a, b);
    }

    public static class BattleResult {
        Player winner;
        Player loser;
        Player loser2;

        public BattleResult(Player a, Player b) {
            if (a.getPower() > b.getPower()) {
                winner = a;
                loser = b;
            } else if (a.getPower() < b.getPower()) {
                winner = b;
                loser = a;
            } else {
                loser = a;
                loser2 = b;
            }
            loser.setAlive(false);
            if (loser2 != null) {
                loser2.setAlive(false);
            }
        }

        public Player getWinner() {
            return winner;
        }

        public Player getLoser() {
            return loser;
        }

        public Player getLoser2() {
            return loser2;
        }
    }
}
